package com.lanwf.adminsuper.sys.login;

import com.jfinal.kit.Ret;
import com.lanwf.adminsuper.sys.utils.MsgKit;
import com.lanwf.adminsuper.sys.utils.SM4;
import com.lanwf.common.model.SysAdmins;

/**
 * @Auther: Lanwf
 * @Date: 2023/8/17 17:57
 * @Description:
 */
public class LoginService {

    public Ret doLogin(String username,String password,Boolean code){
        if(!code) return Ret.fail(MsgKit.CODE_ERROR);;

        SysAdmins sysAdmins = SysAdmins.dao.findFirst("select * from sys_admins where username = ? ", username);
        if(sysAdmins == null) return Ret.fail(MsgKit.USERNAME_PASSWORD_ERROR);

        String pwd = new SM4().encode(password + sysAdmins.getSalt());

        SysAdmins sysAdmin = SysAdmins.dao.findFirst("select * from sys_admins where username = ? and password = ? ", username,pwd);
        if(sysAdmin == null) return Ret.fail(MsgKit.USERNAME_PASSWORD_ERROR);

        return Ret.ok();
    }
}
